<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('下载文件')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang==='zh'?'500px':'600px'" top="5%" @close='close'>
    <el-form ref="form" :model="record" :label-width="lang==='zh'?'100px':'140px'" :rules="rules" label-position="left" style="margin:0 20px;">
      <el-form-item :label="$t('密码')" prop="password">
        <template slot="label">{{$t('密码')}}
          <el-tooltip class="item top" effect="light" :content='content' placement="top">
            <i class="el-icon-warning-outline" style="color:#E6A23C;font-size:16px;"></i>
          </el-tooltip>
        </template>
        <el-input v-emoji v-model="record.password" ref="password" style="width:300px;" size='small' @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
      </el-form-item>
      <el-form-item :label="$t('确认密码')" prop="passwordConfirm">
        <el-input v-emoji v-model="record.passwordConfirm" ref="passwordConfirm" style="width:300px;" size='small' @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { backupDownload } from '@/api/system/configManager'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'DownloadDialog',
  data () {
    var validatePasswordConfirm = (rule, value, callback) => {
      if (this.record.password !== this.record.passwordConfirm) {
        callback(new Error(this.$t('密码两次输入的不一致,请仔细确认!')))
      } else {
        callback()
      }
    }
    var validatePassword = (rule, value, callback) => {
      var dreg = /(?![A-Za-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)(?![\W_]*$)/
      if (!String(value).trim()) {
        callback(new Error(this.$t('不能为空')))
      } else if (value.length < 6 || value.length > 16) {
        callback(new Error(this.$t('密码至少6位，至多16位!')))
      } else if (!dreg.test(value)) {
        callback(new Error(this.$t('至少使用数字、字母、特殊符号中的两种')))
      } else {
        callback()
      }
    }
    return {
      lang: this.$i18n.locale,
      dialogVisible: false,
      content: this.$t('密码中不能包含特殊字符（^;' + "'" + '"' + '`|()[]{}$%*&#!<>\\/）和空格'),
      record: {
        name: '',
        password: '',
        passwordConfirm: ''
      },
      recordCopy: {
        name: '',
        password: '',
        passwordConfirm: ''
      },
      rules: {
        password: [{ required: false, validator: validatePassword, trigger: 'blur' }],
        passwordConfirm: [{ required: false, validator: validatePasswordConfirm, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['password', 'passwordConfirm']
        inputClass(this.$refs, data)
      })
      this.record.name = data.filename
    },
    handleFocus () {
      this.$refs.password = 'password'
    },
    handleBlur () {
      this.$refs.password = 'text'
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          backupDownload(this.record).then(res => {
            if (res[0]) {
              let blob = new Blob([res[0]], { type: res[2] })
              let objectUrl = URL.createObjectURL(blob)
              let link = document.createElement('a')
              link.download = res[1] // 文件名字
              link.href = objectUrl
              // 触发下载
              link.click()
              URL.revokeObjectURL(objectUrl)
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style>
.inputPsd {
  -webkit-text-security: disc;
}
</style>
